package cn.iocoder.yudao.module.enterprise.service.report;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.report.vo.ReportCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.report.vo.ReportPageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.report.vo.ReportRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.report.vo.ReportUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.report.ReportConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.account.AccountDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.controlplan.ControlPlanDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.emissionbasis.BasisValueDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.emissionbasis.EmissionBasisDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.report.ReportDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.account.AccountMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.controlplan.ControlPlanMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.emissionbasis.BasisValueMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.emissionbasis.EmissionBasisMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.report.ReportMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.REPORT_NOT_EXISTS;

/**
 * 核算报告 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportMapper reportMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private BasisValueMapper basisValueMapper;
    @Resource
    private ControlPlanMapper controlPlanMapper;
    @Resource
    private EmissionBasisMapper emissionBasisMapper;

    @Override
    public Long createReport(ReportCreateReqVO createReqVO) {
        // 插入
        ReportDO report = ReportConvert.INSTANCE.convert(createReqVO);
        reportMapper.insert(report);
        // 返回
        return report.getId();
    }

    @Override
    public void updateReport(ReportUpdateReqVO updateReqVO) {
        // 校验存在
        validateReportExists(updateReqVO.getId());
        // 更新
        ReportDO updateObj = ReportConvert.INSTANCE.convert(updateReqVO);
        reportMapper.updateById(updateObj);
    }

    @Override
    public void deleteReport(Long id) {
        // 校验存在
        validateReportExists(id);
        // 删除
        reportMapper.deleteById(id);
    }

    private void validateReportExists(Long id) {
        if (reportMapper.selectById(id) == null) {
            throw exception(REPORT_NOT_EXISTS);
        }
    }

    @Override
    public ReportRespVO getReport(Long id) {
        ReportDO reportDO = reportMapper.selectById(id);
        ReportRespVO respVO = ReportConvert.INSTANCE.convert(reportDO);

        // 拼接核算信息
        AccountDO account = accountMapper.selectById(reportDO.getAccountId());
        respVO.setAccountCycleType(account.getCycleType())
                .setAccountYear(account.getYear())
                .setAccountCycle(account.getCycle())
                .setAccountName(account.getName());
        // 拼接排放基准信息
        EmissionBasisDO emissionBasis = emissionBasisMapper.selectOneByCategory(account.getCycleType());
        BasisValueDO basisValue = basisValueMapper.selectByBasisIdAndTimeInterval(emissionBasis.getId(), account.getCycle());
        respVO.setBasisEmission(basisValue.getTotalRelease());
        // 拼接控制计划信息
        ControlPlanDO controlPlan = controlPlanMapper.selectById(reportDO.getPlanId());
        respVO.setPlanVersion(controlPlan.getVersion());

        return respVO;
    }

    @Override
    public PageResult<ReportDO> getReportPage(ReportPageReqVO pageReqVO) {
        return reportMapper.selectPage(pageReqVO);
    }

}
